#!/usr/bin/python
# -*- coding: utf-8 -*-
# $Revision: 13 $
# $Date: 2012-10-22 20:49:50 +0000 (Mon, 22 Oct 2012) $

import os
import re
import subprocess

path = "/usr/lib/enigma2/python/Plugins/Extensions/DreamDropbox"

if 1 == 1:
    def DropboxList(list):
        res = []
        out = []
        dir = ""
        if len(list) > 1:
            for x in list:
                dir = "%s/%s" % (dir, x)
        else:
            dir = list[0]
        process = subprocess.Popen(["%s/script/dropbox_uploader.sh" % path, "list", "%s"  % dir], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out.append(process.stdout.read().split("\n"))
        if out != '':
            for x in out[0]:
                x = x.strip()
                if x.startswith(">") or x == (""):
                    pass
                else:
                    x = x.replace("\n","")
                    res.append(x)
        res.sort()
        return res
    
if 1 == 1:
    def RealAllDropboxDirectories():
        res = []
        res = DropboxList(".")
        if res >= 1:
            tmp = []
            for x in res:
                tmpx = []
                if x.startswith("[D]"):
                    tmpx = DropboxList("%s" % x.split("[D] ")[1])
                    tmp.append(tmpx)
        
        print tmp
        return res

#list = ["."]
#res = DropboxList(list)
res = RealAllDropboxDirectories()
print "---"
print res